/**
 * Created by zxh on 2016/12/12.
 */
import React from 'react';
import {
  Form,
  Col,
  Input,
  Button,
  DatePicker,
  Select,
  Table,
  Icon,
  Radio,
  InputNumber,
  Checkbox,
  Modal,
  message,
  notification,
  Affix,
  Upload
} from 'antd';

const FormItem = Form.Item;
const Option = Select.Option;

import Logger from '../../utils/Logger';


const logger = Logger.getLogger('BaseElementConverter');

class BaseElementConverter {
  convert(option) {

    const {getFieldProps, field, fieldOptions, wrapper, obj,getFieldDecorator} = option;
    switch (field.dataType) {
      case 'textarea':
        logger.debug('transform field %o to varcharOne input', field);
        return wrapper((
          {...obj.props.form.getFieldDecorator(field.key,fieldOptions)(<Input type="textarea" disabled={field.disabled}  placeholder={field.placeholder} />)}
        ), field);
      case 'select':
        return this.transformSelect(option);
    }
    return this.transformNormal(option);
  }

  /**
   * 将schema中的一列转换为普通输入框
   *
   * @param field
   * @returns {XML}
   */
  transformNormal(option) {
    const {getFieldProps, field, fieldOptions, wrapper, obj} = option;
    switch (field.dataType) {
      case 'int':
        logger.debug('transform field %o to integer input', field);
        return wrapper((
          {...obj.props.form.getFieldDecorator(field.key,fieldOptions)(<InputNumber size="default"  />)}
        ), field);
      case 'float':
        logger.debug('transform field %o to float input', field);
        return wrapper((
          {...obj.props.form.getFieldDecorator(field.key,fieldOptions)(<InputNumber step={0.01} size="default"  placeholder={field.placeholder}  {...getFieldProps(field.key, fieldOptions) } />)}
        ), field);
      case 'datetime':
        logger.debug('transform field %o to datetime input', field);
        return wrapper((
          {...obj.props.form.getFieldDecorator(field.key,fieldOptions)(<DatePicker showTime format="YYYY-MM-DD HH:mm:ss"
            placeholder={field.placeholder || '请选择日期'} />)}
        ), field);
      case 'varchar':  // 默认就是普通的输入框
        logger.debug('transform field %o to varchar input', field);
        return wrapper((
           {...obj.props.form.getFieldDecorator(field.key,fieldOptions)(<Input onClick={field.getModalInnerTable} placeholder={field.placeholder} disabled={field.disabled} size="default"  />)}
        ), field);
      case 'hidden':  // 隐藏表单域
        logger.debug('transform field %o to hidden input', field);
        return wrapper((
          {...obj.props.form.getFieldDecorator(field.key,fieldOptions)(<Input type="hidden" placeholder={field.placeholder} disabled={field.disabled} size="default" />)}
        ), field);
      case 'varTable':
        logger.debug('transform field %o to hidden input', field);
        return wrapper((
          {...obj.props.form.getFieldDecorator(field.key,fieldOptions)(<Col>{field.name}{field.data !== "" ? field.data :<a href='javascript:;'>点击下载</a>}</Col>)}
        ), field);
    }
  }

  /**
   * 将schema中的一列转换为下拉框
   *
   * @param field
   */
  transformSelect(option) {
    const {getFieldProps, field, fieldOptions, wrapper,obj} = option;
    logger.debug('transform field %o to varcharSelect', field);

    let selectOptions = [];

    if (field.options) {
      field.options.forEach((option) => {
        selectOptions.push(<Option value={option.value} key={option.value}>{option.name}</Option>);
      });
    }

    return wrapper((
      {...obj.props.form.getFieldDecorator(field.key,fieldOptions)(<Select size="large" onSelect={field.selectChange} disabled={field.disabled} style={{ width: 200 }}>
        {selectOptions}
      </Select>)}
    ), field);
  }

}

export default BaseElementConverter;
